package com.hzncc.flowable_diboot.flowable.servicetask.refund;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.diboot.core.holder.ThreadLocalHolder;
import com.diboot.core.util.ContextHolder;
import com.hzncc.flowable_diboot.contract.entity.SalesContract;
import com.hzncc.flowable_diboot.contract.service.ContractAuditService;
import com.hzncc.flowable_diboot.contract.service.SalesContractService;
import com.hzncc.flowable_diboot.contract.service.impl.SalesContractServiceImpl;
import com.hzncc.flowable_diboot.entity.refundmanage.RefundContract;
import com.hzncc.flowable_diboot.entity.refundmanage.SapRefundContract;
import com.hzncc.flowable_diboot.service.refundmanage.RefundContractService;
import com.hzncc.flowable_diboot.service.refundmanage.SapRefundContractService;
import com.hzncc.flowable_diboot.service.refundmanage.impl.RefundContractServiceImpl;
import com.hzncc.flowable_diboot.service.refundmanage.impl.SapRefundContractServiceImpl;
import com.hzncc.flowable_diboot.vo.refundmanage.RefundContractDetailVO;
import com.hzncc.flowable_diboot.vo.refundmanage.SapRefundContractDetailVO;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 变更销售合同金额
 */
@Service
public class UpdateRefundSalesContractAmount implements JavaDelegate {

    @Override
    @Transactional
    public void execute(DelegateExecution execution) {
        RefundContractService refundContractService = ContextHolder.getBean(RefundContractServiceImpl.class);
        SalesContractService salesContractService = ContextHolder.getBean(SalesContractServiceImpl.class);
        SapRefundContractService sapRefundContractService = ContextHolder.getBean(SapRefundContractServiceImpl.class);
        String instanceId = execution.getProcessInstanceId();
        LambdaQueryWrapper<RefundContract> refundContractQuery = new LambdaQueryWrapper<>();
        refundContractQuery.eq(RefundContract::getInstanceId,instanceId).eq(RefundContract::isDeleted,0);
        ThreadLocalHolder.setIgnoreInterceptor();
        RefundContractDetailVO refundContractDetailVO = refundContractService.getViewObject(refundContractQuery, RefundContractDetailVO.class);
        LambdaQueryWrapper<SapRefundContract> sapRefundContractQuery = new LambdaQueryWrapper<>();
        sapRefundContractQuery.eq(SapRefundContract::getRefundContractId,refundContractDetailVO.getId()).eq(SapRefundContract::isDeleted,0);
        ThreadLocalHolder.setIgnoreInterceptor();
        List<SapRefundContract> sapRefundContractList = sapRefundContractService.getEntityList(sapRefundContractQuery);
//        List<SapRefundContractDetailVO> sapRefundContractList = refundContractDetailVO.getSapRefundContractList();
        if (!CollectionUtils.isEmpty(sapRefundContractList)){
            for (SapRefundContract sapRefundContractDetailVO : sapRefundContractList){
                String sapContractCode = sapRefundContractDetailVO.getSapContractCode();
                LambdaQueryWrapper<SalesContract> salesContractQuery = new LambdaQueryWrapper<>();
                salesContractQuery.eq(SalesContract::getSapCode,sapContractCode).eq(SalesContract::isDeleted,0);
                SalesContract salesContract = salesContractService.getSingleEntity(salesContractQuery);
                BigDecimal amountReceived = StringUtils.isNotEmpty(salesContract.getAmountReceived())?new BigDecimal(salesContract.getAmountReceived()):new BigDecimal(0);
                BigDecimal amountNotReceived = StringUtils.isNotEmpty(salesContract.getAmountNotReceived())?new BigDecimal(salesContract.getAmountNotReceived()):new BigDecimal(0);
                BigDecimal currentRefundAmount = sapRefundContractDetailVO.getCurrentRefundAmount();//本次退款金额
                if (ObjectUtils.isNotEmpty(amountReceived)&&ObjectUtils.isNotEmpty(amountNotReceived)&&ObjectUtils.isNotEmpty(currentRefundAmount)) {
                    amountReceived = amountReceived.subtract(currentRefundAmount);
                    amountNotReceived = amountNotReceived.add(currentRefundAmount);
                    String paymentStatus = "1";
                    if (amountReceived.compareTo(new BigDecimal(0)) > 0 && amountNotReceived.compareTo(new BigDecimal(0)) > 0) {
                        paymentStatus = "2";
                    } else if (amountNotReceived.compareTo(new BigDecimal(0)) <= 0) {
                        paymentStatus = "3";
                    }
                    LambdaUpdateWrapper<SalesContract> salesContractUpdate = new LambdaUpdateWrapper<>();
                    salesContractUpdate.set(SalesContract::getAmountReceived,amountReceived)
                            .set(SalesContract::getAmountNotReceived,amountNotReceived)
                            .set(SalesContract::getPaymentStatus,paymentStatus)
                            .eq(SalesContract::getSapCode,sapRefundContractDetailVO.getSapContractCode()).eq(SalesContract::isDeleted,0);
                    ThreadLocalHolder.setIgnoreInterceptor();
                    salesContractService.updateEntity(salesContractUpdate);
                    sapRefundContractDetailVO.setReceivedAmount(amountReceived).setUnpaidAmount(amountNotReceived);
                    ThreadLocalHolder.setIgnoreInterceptor();
                    sapRefundContractService.updateEntity(sapRefundContractDetailVO);
                }
            }
        }
    }
}
